import { defineStore } from 'pinia';
import {
  getCategory1ListApi,
  getCategory2ListApi,
  getCategory3ListApi,
} from '/@/api/product/category';
import { Category, Numberable, CategoryListModel } from '/@/api/product/model/categoryModel';

export const useCategoryStore = defineStore({
  id: 'category',
  // state 数据可读可写
  state: (): Category => ({
    category1Id: undefined,
    category2Id: undefined,
    category3Id: undefined,
    category1List: [],
    category2List: [],
    category3List: [],
  }),
  // getters 只读的计算属性数据
  getters: {
    getCategory1Id(): Numberable {
      return this.category1Id;
    },
    getCategory2Id(): Numberable {
      return this.category2Id;
    },
    getCategory3Id(): Numberable {
      return this.category3Id;
    },
    getCategory1List(): CategoryListModel {
      return this.category1List;
    },
    getCategory2List(): CategoryListModel {
      return this.category2List;
    },
    getCategory3List(): CategoryListModel {
      return this.category3List;
    },
  },
  // actions 发送请求更新数据的方法
  // Tip: 如果只需要直接更新数据，直接更新state就OK了，不用定义actions
  actions: {
    async setCategory1List() {
      this.category1List = await getCategory1ListApi();
    },
    async setCategory2List(category1Id: number) {
      this.category2List = await getCategory2ListApi(category1Id);
      this.category1Id = category1Id;
      // 清空数据
      this.category2Id = undefined;
      this.category3Id = undefined;
      this.category3List = [];
    },
    async setCategory3List(category2Id: number) {
      this.category3List = await getCategory3ListApi(category2Id);
      this.category2Id = category2Id;
      this.category3Id = undefined;
    },
  },
});
